{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture\n",
    "!pip install --upgrade wandb\n",
    "!wandb login 3da7a23df9fd940d985adf808de2b09ceb85f15b\n",
    "\n",
    "import wandb\n",
    "wandb.init(project=\"global-wheat-detection\", name='FasterRCNN with ResNet101 backbone: fold0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
    "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5"
   },
   "outputs": [],
   "source": [
    "%%capture\n",
    "!pip install cython\n",
    "!pip install 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'\n",
    "!cp /kaggle/input/rcnnutilswithwandb/engine.py .\n",
    "!cp /kaggle/input/rcnnutilswithwandb/utils.py .\n",
    "!cp /kaggle/input/rcnnutilswithwandb/coco_eval.py .\n",
    "!cp /kaggle/input/rcnnutilswithwandb/coco_utils.py .\n",
    "!cp /kaggle/input/rcnnutilswithwandb/transforms.py ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
    "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import ast\n",
    "import numpy as np # linear algebra\n",
    "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
    "\n",
    "import cv2\n",
    "\n",
    "import torch\n",
    "from PIL import Image\n",
    "from tqdm.auto import tqdm\n",
    "\n",
    "import albumentations\n",
    "from albumentations.pytorch.transforms import ToTensorV2\n",
    "\n",
    "from torch import nn\n",
    "import torchvision\n",
    "import torch.utils.data as data_utils\n",
    "from torch.utils.data import DataLoader, Dataset\n",
    "from torchvision.models.detection.faster_rcnn import FastRCNNPredictor\n",
    "from torchvision.models.detection import FasterRCNN\n",
    "from torchvision.models.detection.rpn import AnchorGenerator\n",
    "\n",
    "from matplotlib import pyplot as plt\n",
    "import matplotlib.patches as patches\n",
    "\n",
    "import utils\n",
    "from engine import train_one_epoch, evaluate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Constants\n",
    "TEST_DIR = '/kaggle/input/global-wheat-detection/test'\n",
    "BASE_DIR = '/kaggle/input/gwdaugmented/train'\n",
    "BATCH_SIZE = 2\n",
    "DEVICE = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n",
    "# our dataset has two classes only - background and wheat heads\n",
    "N_CLASSES = 2\n",
    "N_EPOCHS = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>image_id</th>\n",
       "      <th>x_min</th>\n",
       "      <th>y_min</th>\n",
       "      <th>x_max</th>\n",
       "      <th>y_max</th>\n",
       "      <th>width</th>\n",
       "      <th>height</th>\n",
       "      <th>area</th>\n",
       "      <th>source</th>\n",
       "      <th>kfold</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>834.0</td>\n",
       "      <td>222.0</td>\n",
       "      <td>890.0</td>\n",
       "      <td>258.0</td>\n",
       "      <td>56.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>226.0</td>\n",
       "      <td>548.0</td>\n",
       "      <td>356.0</td>\n",
       "      <td>606.0</td>\n",
       "      <td>130.0</td>\n",
       "      <td>58.0</td>\n",
       "      <td>7540.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>377.0</td>\n",
       "      <td>504.0</td>\n",
       "      <td>451.0</td>\n",
       "      <td>664.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>160.0</td>\n",
       "      <td>11840.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>834.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>943.0</td>\n",
       "      <td>202.0</td>\n",
       "      <td>109.0</td>\n",
       "      <td>107.0</td>\n",
       "      <td>11663.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>26.0</td>\n",
       "      <td>144.0</td>\n",
       "      <td>150.0</td>\n",
       "      <td>261.0</td>\n",
       "      <td>124.0</td>\n",
       "      <td>117.0</td>\n",
       "      <td>14508.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295533</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>876.0</td>\n",
       "      <td>619.0</td>\n",
       "      <td>960.0</td>\n",
       "      <td>714.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>7980.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295534</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>625.0</td>\n",
       "      <td>549.0</td>\n",
       "      <td>732.0</td>\n",
       "      <td>631.0</td>\n",
       "      <td>107.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>8774.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295535</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>749.0</td>\n",
       "      <td>228.0</td>\n",
       "      <td>890.0</td>\n",
       "      <td>299.0</td>\n",
       "      <td>141.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>10011.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295536</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>410.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>594.0</td>\n",
       "      <td>92.0</td>\n",
       "      <td>184.0</td>\n",
       "      <td>79.0</td>\n",
       "      <td>14536.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295537</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>55.0</td>\n",
       "      <td>740.0</td>\n",
       "      <td>149.0</td>\n",
       "      <td>801.0</td>\n",
       "      <td>94.0</td>\n",
       "      <td>61.0</td>\n",
       "      <td>5734.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>295538 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             image_id  x_min  y_min  x_max  y_max  width  height     area  \\\n",
       "0           b6ab77fd7  834.0  222.0  890.0  258.0   56.0    36.0   2016.0   \n",
       "1           b6ab77fd7  226.0  548.0  356.0  606.0  130.0    58.0   7540.0   \n",
       "2           b6ab77fd7  377.0  504.0  451.0  664.0   74.0   160.0  11840.0   \n",
       "3           b6ab77fd7  834.0   95.0  943.0  202.0  109.0   107.0  11663.0   \n",
       "4           b6ab77fd7   26.0  144.0  150.0  261.0  124.0   117.0  14508.0   \n",
       "...               ...    ...    ...    ...    ...    ...     ...      ...   \n",
       "295533  5e0747034_aug  876.0  619.0  960.0  714.0   84.0    95.0   7980.0   \n",
       "295534  5e0747034_aug  625.0  549.0  732.0  631.0  107.0    82.0   8774.0   \n",
       "295535  5e0747034_aug  749.0  228.0  890.0  299.0  141.0    71.0  10011.0   \n",
       "295536  5e0747034_aug  410.0   13.0  594.0   92.0  184.0    79.0  14536.0   \n",
       "295537  5e0747034_aug   55.0  740.0  149.0  801.0   94.0    61.0   5734.0   \n",
       "\n",
       "           source  kfold  \n",
       "0         usask_1      2  \n",
       "1         usask_1      2  \n",
       "2         usask_1      2  \n",
       "3         usask_1      2  \n",
       "4         usask_1      2  \n",
       "...           ...    ...  \n",
       "295533  arvalis_2      1  \n",
       "295534  arvalis_2      1  \n",
       "295535  arvalis_2      1  \n",
       "295536  arvalis_2      1  \n",
       "295537  arvalis_2      1  \n",
       "\n",
       "[295538 rows x 10 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df = pd.read_csv(os.path.join('/kaggle/input/gwdaugmented/', 'train.csv'))\n",
    "train_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>image_id</th>\n",
       "      <th>x_min</th>\n",
       "      <th>y_min</th>\n",
       "      <th>x_max</th>\n",
       "      <th>y_max</th>\n",
       "      <th>width</th>\n",
       "      <th>height</th>\n",
       "      <th>area</th>\n",
       "      <th>source</th>\n",
       "      <th>kfold</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>834.0</td>\n",
       "      <td>222.0</td>\n",
       "      <td>890.0</td>\n",
       "      <td>258.0</td>\n",
       "      <td>56.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>226.0</td>\n",
       "      <td>548.0</td>\n",
       "      <td>356.0</td>\n",
       "      <td>606.0</td>\n",
       "      <td>130.0</td>\n",
       "      <td>58.0</td>\n",
       "      <td>7540.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>377.0</td>\n",
       "      <td>504.0</td>\n",
       "      <td>451.0</td>\n",
       "      <td>664.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>160.0</td>\n",
       "      <td>11840.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>834.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>943.0</td>\n",
       "      <td>202.0</td>\n",
       "      <td>109.0</td>\n",
       "      <td>107.0</td>\n",
       "      <td>11663.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>26.0</td>\n",
       "      <td>144.0</td>\n",
       "      <td>150.0</td>\n",
       "      <td>261.0</td>\n",
       "      <td>124.0</td>\n",
       "      <td>117.0</td>\n",
       "      <td>14508.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295533</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>876.0</td>\n",
       "      <td>619.0</td>\n",
       "      <td>960.0</td>\n",
       "      <td>714.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>7980.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295534</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>625.0</td>\n",
       "      <td>549.0</td>\n",
       "      <td>732.0</td>\n",
       "      <td>631.0</td>\n",
       "      <td>107.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>8774.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295535</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>749.0</td>\n",
       "      <td>228.0</td>\n",
       "      <td>890.0</td>\n",
       "      <td>299.0</td>\n",
       "      <td>141.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>10011.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295536</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>410.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>594.0</td>\n",
       "      <td>92.0</td>\n",
       "      <td>184.0</td>\n",
       "      <td>79.0</td>\n",
       "      <td>14536.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295537</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>55.0</td>\n",
       "      <td>740.0</td>\n",
       "      <td>149.0</td>\n",
       "      <td>801.0</td>\n",
       "      <td>94.0</td>\n",
       "      <td>61.0</td>\n",
       "      <td>5734.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>295538 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             image_id  x_min  y_min  x_max  y_max  width  height     area  \\\n",
       "0           b6ab77fd7  834.0  222.0  890.0  258.0   56.0    36.0   2016.0   \n",
       "1           b6ab77fd7  226.0  548.0  356.0  606.0  130.0    58.0   7540.0   \n",
       "2           b6ab77fd7  377.0  504.0  451.0  664.0   74.0   160.0  11840.0   \n",
       "3           b6ab77fd7  834.0   95.0  943.0  202.0  109.0   107.0  11663.0   \n",
       "4           b6ab77fd7   26.0  144.0  150.0  261.0  124.0   117.0  14508.0   \n",
       "...               ...    ...    ...    ...    ...    ...     ...      ...   \n",
       "295533  5e0747034_aug  876.0  619.0  960.0  714.0   84.0    95.0   7980.0   \n",
       "295534  5e0747034_aug  625.0  549.0  732.0  631.0  107.0    82.0   8774.0   \n",
       "295535  5e0747034_aug  749.0  228.0  890.0  299.0  141.0    71.0  10011.0   \n",
       "295536  5e0747034_aug  410.0   13.0  594.0   92.0  184.0    79.0  14536.0   \n",
       "295537  5e0747034_aug   55.0  740.0  149.0  801.0   94.0    61.0   5734.0   \n",
       "\n",
       "           source  kfold  \n",
       "0         usask_1      2  \n",
       "1         usask_1      2  \n",
       "2         usask_1      2  \n",
       "3         usask_1      2  \n",
       "4         usask_1      2  \n",
       "...           ...    ...  \n",
       "295533  arvalis_2      1  \n",
       "295534  arvalis_2      1  \n",
       "295535  arvalis_2      1  \n",
       "295536  arvalis_2      1  \n",
       "295537  arvalis_2      1  \n",
       "\n",
       "[295538 rows x 10 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "class WheatDataset(Dataset):\n",
    "    \n",
    "    def __init__(self, df, folds, transforms=None):\n",
    "        self.df = df[df.kfold.isin(folds)].reset_index(drop=True)\n",
    "        self.image_ids = self.df['image_id'].unique()\n",
    "        self.transforms = transforms\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.image_ids)\n",
    "    \n",
    "    def __getitem__(self, index):\n",
    "        image_id = self.image_ids[index]\n",
    "        image = cv2.imread(os.path.join(BASE_DIR, 'train', f'{image_id}.jpg'), cv2.IMREAD_COLOR)\n",
    "        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n",
    "        image /= 255.0\n",
    "\n",
    "        # Convert from NHWC to NCHW as pytorch expects images in NCHW format\n",
    "        image = np.transpose(image, (2, 0, 1))\n",
    "        image = torch.from_numpy(image)\n",
    "        \n",
    "        # Get bbox coordinates for each wheat head(s)\n",
    "        bboxes_df = self.df[self.df['image_id'] == image_id]\n",
    "        boxes, areas = [], []\n",
    "        n_objects = len(bboxes_df)  # Number of wheat heads in the given image\n",
    "\n",
    "        for i in range(n_objects):\n",
    "            x_min = bboxes_df.iloc[i]['x_min']\n",
    "            x_max = bboxes_df.iloc[i]['x_max']\n",
    "            y_min = bboxes_df.iloc[i]['y_min']\n",
    "            y_max = bboxes_df.iloc[i]['y_max']\n",
    "\n",
    "            boxes.append([x_min, y_min, x_max, y_max])\n",
    "            areas.append(bboxes_df.iloc[i]['area'])\n",
    "\n",
    "        boxes = torch.as_tensor(boxes, dtype=torch.int64)\n",
    "        \n",
    "        # Get the labels. We have only one class (wheat head)\n",
    "        labels = torch.ones((n_objects, ), dtype=torch.int64)\n",
    "        \n",
    "        areas = torch.as_tensor(areas)\n",
    "        \n",
    "        # suppose all instances are not crowd\n",
    "        iscrowd = torch.zeros((n_objects, ), dtype=torch.int64)\n",
    "        \n",
    "        target = {\n",
    "            'boxes': boxes,\n",
    "            'labels': labels,\n",
    "            'image_id': torch.tensor([index]),\n",
    "            'area': areas,\n",
    "            'iscrowd': iscrowd\n",
    "        }\n",
    "        \n",
    "        if self.transforms:\n",
    "            result_aug = self.transforms(image=image, bboxes=boxes, labels=labels)\n",
    "            image = result_aug['image'].float()\n",
    "            \n",
    "            target['boxes'] = torch.stack(tuple(map(torch.tensor, zip(*result_aug['bboxes'])))).permute(1, 0)\n",
    "\n",
    "        return image, target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_model(pre_trained=True):\n",
    "    \n",
    "    # Reference: https://stackoverflow.com/questions/58362892/resnet-18-as-backbone-in-faster-r-cnn\n",
    "    resnet_net = torchvision.models.resnet101(pretrained=True) \n",
    "    modules = list(resnet_net.children())[:-2]\n",
    "\n",
    "    backbone = nn.Sequential(*modules)\n",
    "    backbone.out_channels = 2048\n",
    "\n",
    "    # let's make the RPN generate 5 x 3 anchors per spatial\n",
    "    # location, with 5 different sizes and 3 different aspect\n",
    "    # ratios. We have a Tuple[Tuple[int]] because each feature\n",
    "    # map could potentially have different sizes and\n",
    "    # aspect ratios\n",
    "    anchor_generator = AnchorGenerator(sizes=((32, 64, 128, 256, 512),),\n",
    "                                       aspect_ratios=((0.5, 1.0, 2.0),))\n",
    "\n",
    "    # put the pieces together inside a FasterRCNN model\n",
    "    model = FasterRCNN(backbone,\n",
    "                       num_classes=N_CLASSES,\n",
    "                       rpn_anchor_generator=anchor_generator)\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture\n",
    "# get the model using our helper function\n",
    "model = get_model()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.7/site-packages/torch/nn/functional.py:2854: UserWarning: The default behavior for interpolate/upsample with float scale_factor will change in 1.6.0 to align with other frameworks/libraries, and use scale_factor directly, instead of relying on the computed output size. If you wish to keep the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details. \n",
      "  warnings.warn(\"The default behavior for interpolate/upsample with float scale_factor will change \"\n",
      "/opt/conda/conda-bld/pytorch_1587428398394/work/torch/csrc/utils/python_arg_parser.cpp:756: UserWarning: This overload of nonzero is deprecated:\n",
      "\tnonzero(Tensor input, *, Tensor out)\n",
      "Consider using one of the following signatures instead:\n",
      "\tnonzero(Tensor input, *, bool as_tuple)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: [0]  [   0/2698]  eta: 2:16:31  lr: 0.000010  loss: 1.6243 (1.6243)  loss_classifier: 0.6842 (0.6842)  loss_box_reg: 0.0828 (0.0828)  loss_objectness: 0.6968 (0.6968)  loss_rpn_box_reg: 0.1605 (0.1605)  time: 3.0362  data: 0.9329  max mem: 5070\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.7/site-packages/torch/nn/functional.py:2854: UserWarning: The default behavior for interpolate/upsample with float scale_factor will change in 1.6.0 to align with other frameworks/libraries, and use scale_factor directly, instead of relying on the computed output size. If you wish to keep the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details. \n",
      "  warnings.warn(\"The default behavior for interpolate/upsample with float scale_factor will change \"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: [0]  [ 100/2698]  eta: 0:23:30  lr: 0.000509  loss: 1.1600 (1.3703)  loss_classifier: 0.3955 (0.4717)  loss_box_reg: 0.2091 (0.1999)  loss_objectness: 0.3263 (0.4973)  loss_rpn_box_reg: 0.1693 (0.2013)  time: 0.5156  data: 0.0113  max mem: 6482\n",
      "Epoch: [0]  [ 200/2698]  eta: 0:25:04  lr: 0.001009  loss: 1.1146 (1.2749)  loss_classifier: 0.3681 (0.4307)  loss_box_reg: 0.3327 (0.2467)  loss_objectness: 0.2515 (0.4037)  loss_rpn_box_reg: 0.1483 (0.1938)  time: 0.9699  data: 0.4572  max mem: 6482\n",
      "Epoch: [0]  [ 300/2698]  eta: 0:22:52  lr: 0.001508  loss: 1.1144 (1.2197)  loss_classifier: 0.3609 (0.4081)  loss_box_reg: 0.3906 (0.2849)  loss_objectness: 0.2250 (0.3470)  loss_rpn_box_reg: 0.1400 (0.1796)  time: 0.5207  data: 0.0113  max mem: 6482\n",
      "Epoch: [0]  [ 400/2698]  eta: 0:21:21  lr: 0.002008  loss: 1.0847 (1.1805)  loss_classifier: 0.3624 (0.3962)  loss_box_reg: 0.3816 (0.3092)  loss_objectness: 0.1622 (0.3046)  loss_rpn_box_reg: 0.1302 (0.1705)  time: 0.5183  data: 0.0119  max mem: 6482\n",
      "Epoch: [0]  [ 500/2698]  eta: 0:20:06  lr: 0.002507  loss: 0.9783 (1.1464)  loss_classifier: 0.3455 (0.3864)  loss_box_reg: 0.3749 (0.3206)  loss_objectness: 0.1211 (0.2755)  loss_rpn_box_reg: 0.1129 (0.1639)  time: 0.5149  data: 0.0109  max mem: 6482\n",
      "Epoch: [0]  [ 600/2698]  eta: 0:18:59  lr: 0.003007  loss: 0.8497 (1.1060)  loss_classifier: 0.2811 (0.3729)  loss_box_reg: 0.3327 (0.3250)  loss_objectness: 0.1080 (0.2505)  loss_rpn_box_reg: 0.1012 (0.1576)  time: 0.5116  data: 0.0099  max mem: 6482\n",
      "Epoch: [0]  [ 700/2698]  eta: 0:17:57  lr: 0.003506  loss: 0.8297 (1.0697)  loss_classifier: 0.2856 (0.3619)  loss_box_reg: 0.3323 (0.3257)  loss_objectness: 0.0920 (0.2310)  loss_rpn_box_reg: 0.1013 (0.1511)  time: 0.5101  data: 0.0101  max mem: 6482\n",
      "Epoch: [0]  [ 800/2698]  eta: 0:16:57  lr: 0.004006  loss: 0.7649 (1.0371)  loss_classifier: 0.2804 (0.3521)  loss_box_reg: 0.3055 (0.3251)  loss_objectness: 0.0881 (0.2146)  loss_rpn_box_reg: 0.1002 (0.1453)  time: 0.5105  data: 0.0099  max mem: 6482\n",
      "Epoch: [0]  [ 900/2698]  eta: 0:16:04  lr: 0.004505  loss: 0.8211 (1.0135)  loss_classifier: 0.2992 (0.3453)  loss_box_reg: 0.3410 (0.3246)  loss_objectness: 0.0957 (0.2020)  loss_rpn_box_reg: 0.1190 (0.1415)  time: 0.6436  data: 0.1424  max mem: 6482\n",
      "Epoch: [0]  [1000/2698]  eta: 0:15:07  lr: 0.005000  loss: 0.7578 (0.9924)  loss_classifier: 0.2744 (0.3399)  loss_box_reg: 0.3000 (0.3226)  loss_objectness: 0.0859 (0.1915)  loss_rpn_box_reg: 0.1083 (0.1384)  time: 0.5139  data: 0.0104  max mem: 6482\n",
      "Epoch: [0]  [1100/2698]  eta: 0:14:10  lr: 0.005000  loss: 0.7180 (0.9688)  loss_classifier: 0.2658 (0.3333)  loss_box_reg: 0.2740 (0.3194)  loss_objectness: 0.0808 (0.1819)  loss_rpn_box_reg: 0.0832 (0.1342)  time: 0.5226  data: 0.0105  max mem: 6482\n",
      "Epoch: [0]  [1200/2698]  eta: 0:13:14  lr: 0.005000  loss: 0.7556 (0.9502)  loss_classifier: 0.2756 (0.3280)  loss_box_reg: 0.2935 (0.3176)  loss_objectness: 0.0866 (0.1736)  loss_rpn_box_reg: 0.0923 (0.1311)  time: 0.5091  data: 0.0100  max mem: 6482\n",
      "Epoch: [0]  [1300/2698]  eta: 0:12:19  lr: 0.005000  loss: 0.7023 (0.9319)  loss_classifier: 0.2632 (0.3230)  loss_box_reg: 0.2750 (0.3145)  loss_objectness: 0.0657 (0.1664)  loss_rpn_box_reg: 0.0720 (0.1281)  time: 0.5094  data: 0.0100  max mem: 6482\n",
      "Epoch: [0]  [1400/2698]  eta: 0:11:25  lr: 0.005000  loss: 0.7464 (0.9159)  loss_classifier: 0.2618 (0.3186)  loss_box_reg: 0.2680 (0.3118)  loss_objectness: 0.0728 (0.1601)  loss_rpn_box_reg: 0.1023 (0.1254)  time: 0.5117  data: 0.0102  max mem: 6482\n",
      "Epoch: [0]  [1500/2698]  eta: 0:10:31  lr: 0.005000  loss: 0.6922 (0.9011)  loss_classifier: 0.2720 (0.3145)  loss_box_reg: 0.2675 (0.3090)  loss_objectness: 0.0674 (0.1544)  loss_rpn_box_reg: 0.0985 (0.1233)  time: 0.5111  data: 0.0104  max mem: 6482\n",
      "Epoch: [0]  [1600/2698]  eta: 0:09:37  lr: 0.005000  loss: 0.6851 (0.8857)  loss_classifier: 0.2557 (0.3104)  loss_box_reg: 0.2699 (0.3057)  loss_objectness: 0.0610 (0.1489)  loss_rpn_box_reg: 0.0779 (0.1206)  time: 0.5107  data: 0.0102  max mem: 6482\n",
      "Epoch: [0]  [1700/2698]  eta: 0:08:44  lr: 0.005000  loss: 0.6532 (0.8725)  loss_classifier: 0.2491 (0.3068)  loss_box_reg: 0.2416 (0.3031)  loss_objectness: 0.0599 (0.1443)  loss_rpn_box_reg: 0.0894 (0.1183)  time: 0.5138  data: 0.0103  max mem: 6482\n",
      "Epoch: [0]  [1800/2698]  eta: 0:07:51  lr: 0.005000  loss: 0.6464 (0.8604)  loss_classifier: 0.2424 (0.3034)  loss_box_reg: 0.2545 (0.3001)  loss_objectness: 0.0635 (0.1403)  loss_rpn_box_reg: 0.0830 (0.1166)  time: 0.5173  data: 0.0103  max mem: 6482\n",
      "Epoch: [0]  [1900/2698]  eta: 0:06:57  lr: 0.005000  loss: 0.6213 (0.8485)  loss_classifier: 0.2567 (0.3002)  loss_box_reg: 0.2439 (0.2974)  loss_objectness: 0.0553 (0.1363)  loss_rpn_box_reg: 0.0660 (0.1146)  time: 0.5092  data: 0.0099  max mem: 6482\n",
      "Epoch: [0]  [2000/2698]  eta: 0:06:05  lr: 0.005000  loss: 0.6081 (0.8385)  loss_classifier: 0.2327 (0.2976)  loss_box_reg: 0.2224 (0.2949)  loss_objectness: 0.0584 (0.1328)  loss_rpn_box_reg: 0.0746 (0.1132)  time: 0.5089  data: 0.0100  max mem: 6482\n",
      "Epoch: [0]  [2100/2698]  eta: 0:05:12  lr: 0.005000  loss: 0.6303 (0.8286)  loss_classifier: 0.2354 (0.2949)  loss_box_reg: 0.2521 (0.2924)  loss_objectness: 0.0536 (0.1295)  loss_rpn_box_reg: 0.0863 (0.1118)  time: 0.5102  data: 0.0098  max mem: 6482\n",
      "Epoch: [0]  [2200/2698]  eta: 0:04:20  lr: 0.005000  loss: 0.6418 (0.8190)  loss_classifier: 0.2370 (0.2922)  loss_box_reg: 0.2485 (0.2902)  loss_objectness: 0.0648 (0.1264)  loss_rpn_box_reg: 0.0871 (0.1103)  time: 0.5071  data: 0.0095  max mem: 6482\n",
      "Epoch: [0]  [2300/2698]  eta: 0:03:27  lr: 0.005000  loss: 0.5604 (0.8090)  loss_classifier: 0.2194 (0.2893)  loss_box_reg: 0.2392 (0.2877)  loss_objectness: 0.0440 (0.1233)  loss_rpn_box_reg: 0.0638 (0.1087)  time: 0.5102  data: 0.0108  max mem: 6482\n",
      "Epoch: [0]  [2400/2698]  eta: 0:02:35  lr: 0.005000  loss: 0.6037 (0.8007)  loss_classifier: 0.2241 (0.2871)  loss_box_reg: 0.2358 (0.2854)  loss_objectness: 0.0617 (0.1207)  loss_rpn_box_reg: 0.0755 (0.1076)  time: 0.5235  data: 0.0106  max mem: 6482\n",
      "Epoch: [0]  [2500/2698]  eta: 0:01:43  lr: 0.005000  loss: 0.5988 (0.7928)  loss_classifier: 0.2242 (0.2849)  loss_box_reg: 0.2298 (0.2833)  loss_objectness: 0.0527 (0.1181)  loss_rpn_box_reg: 0.0773 (0.1064)  time: 0.5193  data: 0.0098  max mem: 6482\n",
      "Epoch: [0]  [2600/2698]  eta: 0:00:51  lr: 0.005000  loss: 0.5749 (0.7854)  loss_classifier: 0.2174 (0.2829)  loss_box_reg: 0.2130 (0.2814)  loss_objectness: 0.0532 (0.1159)  loss_rpn_box_reg: 0.0801 (0.1053)  time: 0.5088  data: 0.0099  max mem: 6482\n",
      "Epoch: [0]  [2697/2698]  eta: 0:00:00  lr: 0.005000  loss: 0.5238 (0.7782)  loss_classifier: 0.2008 (0.2808)  loss_box_reg: 0.2003 (0.2795)  loss_objectness: 0.0398 (0.1137)  loss_rpn_box_reg: 0.0569 (0.1042)  time: 0.5030  data: 0.0099  max mem: 6482\n",
      "Epoch: [0] Total time: 0:23:23 (0.5204 s / it)\n",
      "creating index...\n",
      "index created!\n",
      "Test:  [  0/675]  eta: 0:12:17  model_time: 0.2452 (0.2452)  evaluator_time: 0.3493 (0.3493)  time: 1.0932  data: 0.4800  max mem: 6482\n",
      "Test:  [100/675]  eta: 0:05:10  model_time: 0.1550 (0.1579)  evaluator_time: 0.3089 (0.3577)  time: 0.4946  data: 0.0117  max mem: 6482\n",
      "Test:  [200/675]  eta: 0:04:49  model_time: 0.1554 (0.1575)  evaluator_time: 0.7270 (0.4309)  time: 0.9198  data: 0.0106  max mem: 6482\n",
      "Test:  [300/675]  eta: 0:03:38  model_time: 0.1561 (0.1582)  evaluator_time: 0.3427 (0.4028)  time: 0.5228  data: 0.0109  max mem: 6482\n",
      "Test:  [400/675]  eta: 0:02:33  model_time: 0.1547 (0.1583)  evaluator_time: 0.5111 (0.3803)  time: 0.7067  data: 0.0114  max mem: 6482\n",
      "Test:  [500/675]  eta: 0:01:37  model_time: 0.1588 (0.1583)  evaluator_time: 0.7602 (0.3814)  time: 0.9526  data: 0.0104  max mem: 6482\n",
      "Test:  [600/675]  eta: 0:00:43  model_time: 0.1585 (0.1583)  evaluator_time: 0.2200 (0.4045)  time: 0.4711  data: 0.0121  max mem: 6482\n",
      "Test:  [674/675]  eta: 0:00:00  model_time: 0.1597 (0.1583)  evaluator_time: 0.1196 (0.3904)  time: 0.3140  data: 0.0105  max mem: 6482\n",
      "Test: Total time: 0:06:24 (0.5689 s / it)\n",
      "Averaged stats: model_time: 0.1597 (0.1583)  evaluator_time: 0.1196 (0.3904)\n",
      "Accumulating evaluation results...\n",
      "DONE (t=1.73s).\n",
      "IoU metric: bbox\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.325\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.810\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.179\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.024\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.297\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.445\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.013\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.121\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.414\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.064\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.380\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.563\n",
      "Epoch: [1]  [   0/2698]  eta: 1:16:15  lr: 0.005000  loss: 0.5365 (0.5365)  loss_classifier: 0.2002 (0.2002)  loss_box_reg: 0.2065 (0.2065)  loss_objectness: 0.0448 (0.0448)  loss_rpn_box_reg: 0.0849 (0.0849)  time: 1.6960  data: 1.0007  max mem: 6482\n",
      "Epoch: [1]  [ 100/2698]  eta: 0:22:43  lr: 0.005000  loss: 0.5560 (0.5778)  loss_classifier: 0.2073 (0.2233)  loss_box_reg: 0.2212 (0.2250)  loss_objectness: 0.0593 (0.0537)  loss_rpn_box_reg: 0.0746 (0.0758)  time: 0.5081  data: 0.0097  max mem: 6482\n",
      "Epoch: [1]  [ 200/2698]  eta: 0:21:36  lr: 0.005000  loss: 0.5610 (0.5706)  loss_classifier: 0.2217 (0.2206)  loss_box_reg: 0.2167 (0.2207)  loss_objectness: 0.0471 (0.0539)  loss_rpn_box_reg: 0.0691 (0.0755)  time: 0.5111  data: 0.0102  max mem: 6482\n",
      "Epoch: [1]  [ 300/2698]  eta: 0:20:40  lr: 0.005000  loss: 0.5093 (0.5664)  loss_classifier: 0.1963 (0.2183)  loss_box_reg: 0.2049 (0.2204)  loss_objectness: 0.0446 (0.0528)  loss_rpn_box_reg: 0.0587 (0.0749)  time: 0.5118  data: 0.0103  max mem: 6482\n",
      "Epoch: [1]  [ 400/2698]  eta: 0:19:46  lr: 0.005000  loss: 0.5505 (0.5688)  loss_classifier: 0.2179 (0.2196)  loss_box_reg: 0.2157 (0.2214)  loss_objectness: 0.0460 (0.0526)  loss_rpn_box_reg: 0.0685 (0.0753)  time: 0.5291  data: 0.0120  max mem: 6482\n",
      "Epoch: [1]  [ 500/2698]  eta: 0:18:53  lr: 0.005000  loss: 0.5442 (0.5698)  loss_classifier: 0.2029 (0.2202)  loss_box_reg: 0.2096 (0.2214)  loss_objectness: 0.0503 (0.0531)  loss_rpn_box_reg: 0.0693 (0.0752)  time: 0.5198  data: 0.0118  max mem: 6482\n",
      "Epoch: [1]  [ 600/2698]  eta: 0:18:00  lr: 0.005000  loss: 0.5294 (0.5656)  loss_classifier: 0.1984 (0.2191)  loss_box_reg: 0.2060 (0.2195)  loss_objectness: 0.0446 (0.0523)  loss_rpn_box_reg: 0.0675 (0.0746)  time: 0.5118  data: 0.0102  max mem: 6482\n",
      "Epoch: [1]  [ 700/2698]  eta: 0:17:08  lr: 0.005000  loss: 0.5267 (0.5637)  loss_classifier: 0.1952 (0.2181)  loss_box_reg: 0.2194 (0.2194)  loss_objectness: 0.0470 (0.0522)  loss_rpn_box_reg: 0.0661 (0.0740)  time: 0.5100  data: 0.0100  max mem: 6482\n",
      "Epoch: [1]  [ 800/2698]  eta: 0:16:16  lr: 0.005000  loss: 0.5699 (0.5635)  loss_classifier: 0.2272 (0.2179)  loss_box_reg: 0.2208 (0.2198)  loss_objectness: 0.0407 (0.0522)  loss_rpn_box_reg: 0.0701 (0.0737)  time: 0.5080  data: 0.0099  max mem: 6482\n",
      "Epoch: [1]  [ 900/2698]  eta: 0:15:24  lr: 0.005000  loss: 0.5703 (0.5635)  loss_classifier: 0.2309 (0.2183)  loss_box_reg: 0.2159 (0.2196)  loss_objectness: 0.0468 (0.0521)  loss_rpn_box_reg: 0.0690 (0.0735)  time: 0.5093  data: 0.0099  max mem: 6482\n",
      "Epoch: [1]  [1000/2698]  eta: 0:14:33  lr: 0.005000  loss: 0.5655 (0.5616)  loss_classifier: 0.2143 (0.2179)  loss_box_reg: 0.2334 (0.2184)  loss_objectness: 0.0415 (0.0519)  loss_rpn_box_reg: 0.0659 (0.0734)  time: 0.5128  data: 0.0106  max mem: 6482\n",
      "Epoch: [1]  [1100/2698]  eta: 0:13:41  lr: 0.005000  loss: 0.5171 (0.5589)  loss_classifier: 0.2035 (0.2172)  loss_box_reg: 0.1937 (0.2177)  loss_objectness: 0.0466 (0.0512)  loss_rpn_box_reg: 0.0718 (0.0728)  time: 0.5311  data: 0.0133  max mem: 6482\n",
      "Epoch: [1]  [1200/2698]  eta: 0:12:49  lr: 0.005000  loss: 0.5497 (0.5559)  loss_classifier: 0.2146 (0.2163)  loss_box_reg: 0.2108 (0.2166)  loss_objectness: 0.0413 (0.0506)  loss_rpn_box_reg: 0.0731 (0.0725)  time: 0.5088  data: 0.0100  max mem: 6482\n",
      "Epoch: [1]  [1300/2698]  eta: 0:11:58  lr: 0.005000  loss: 0.5487 (0.5557)  loss_classifier: 0.2094 (0.2162)  loss_box_reg: 0.2013 (0.2164)  loss_objectness: 0.0485 (0.0508)  loss_rpn_box_reg: 0.0760 (0.0724)  time: 0.5121  data: 0.0113  max mem: 6482\n",
      "Epoch: [1]  [1400/2698]  eta: 0:11:06  lr: 0.005000  loss: 0.5390 (0.5545)  loss_classifier: 0.2205 (0.2160)  loss_box_reg: 0.2236 (0.2159)  loss_objectness: 0.0444 (0.0505)  loss_rpn_box_reg: 0.0620 (0.0722)  time: 0.5098  data: 0.0107  max mem: 6482\n",
      "Epoch: [1]  [1500/2698]  eta: 0:10:15  lr: 0.005000  loss: 0.5278 (0.5532)  loss_classifier: 0.2113 (0.2156)  loss_box_reg: 0.2104 (0.2154)  loss_objectness: 0.0395 (0.0504)  loss_rpn_box_reg: 0.0596 (0.0718)  time: 0.5101  data: 0.0104  max mem: 6482\n",
      "Epoch: [1]  [1600/2698]  eta: 0:09:24  lr: 0.005000  loss: 0.5433 (0.5513)  loss_classifier: 0.2139 (0.2150)  loss_box_reg: 0.2109 (0.2144)  loss_objectness: 0.0446 (0.0502)  loss_rpn_box_reg: 0.0632 (0.0717)  time: 0.5091  data: 0.0103  max mem: 6482\n",
      "Epoch: [1]  [1700/2698]  eta: 0:08:32  lr: 0.005000  loss: 0.5619 (0.5495)  loss_classifier: 0.2107 (0.2145)  loss_box_reg: 0.2050 (0.2135)  loss_objectness: 0.0539 (0.0499)  loss_rpn_box_reg: 0.0834 (0.0715)  time: 0.5151  data: 0.0109  max mem: 6482\n",
      "Epoch: [1]  [1800/2698]  eta: 0:07:41  lr: 0.005000  loss: 0.4867 (0.5489)  loss_classifier: 0.2030 (0.2141)  loss_box_reg: 0.1929 (0.2133)  loss_objectness: 0.0358 (0.0499)  loss_rpn_box_reg: 0.0518 (0.0716)  time: 0.5242  data: 0.0121  max mem: 6482\n",
      "Epoch: [1]  [1900/2698]  eta: 0:06:49  lr: 0.005000  loss: 0.5448 (0.5477)  loss_classifier: 0.2140 (0.2137)  loss_box_reg: 0.2169 (0.2128)  loss_objectness: 0.0435 (0.0498)  loss_rpn_box_reg: 0.0710 (0.0714)  time: 0.5094  data: 0.0101  max mem: 6482\n",
      "Epoch: [1]  [2000/2698]  eta: 0:05:58  lr: 0.005000  loss: 0.5126 (0.5469)  loss_classifier: 0.1972 (0.2134)  loss_box_reg: 0.1994 (0.2126)  loss_objectness: 0.0390 (0.0496)  loss_rpn_box_reg: 0.0610 (0.0712)  time: 0.5123  data: 0.0119  max mem: 6482\n",
      "Epoch: [1]  [2100/2698]  eta: 0:05:07  lr: 0.005000  loss: 0.5559 (0.5469)  loss_classifier: 0.2213 (0.2134)  loss_box_reg: 0.2216 (0.2125)  loss_objectness: 0.0499 (0.0498)  loss_rpn_box_reg: 0.0727 (0.0712)  time: 0.5112  data: 0.0100  max mem: 6482\n",
      "Epoch: [1]  [2200/2698]  eta: 0:04:15  lr: 0.005000  loss: 0.5017 (0.5460)  loss_classifier: 0.2033 (0.2131)  loss_box_reg: 0.2057 (0.2121)  loss_objectness: 0.0347 (0.0497)  loss_rpn_box_reg: 0.0592 (0.0711)  time: 0.5126  data: 0.0111  max mem: 6482\n",
      "Epoch: [1]  [2300/2698]  eta: 0:03:24  lr: 0.005000  loss: 0.5194 (0.5444)  loss_classifier: 0.2105 (0.2125)  loss_box_reg: 0.1991 (0.2115)  loss_objectness: 0.0362 (0.0495)  loss_rpn_box_reg: 0.0587 (0.0708)  time: 0.5099  data: 0.0106  max mem: 6482\n",
      "Epoch: [1]  [2400/2698]  eta: 0:02:33  lr: 0.005000  loss: 0.5745 (0.5449)  loss_classifier: 0.2138 (0.2128)  loss_box_reg: 0.2095 (0.2115)  loss_objectness: 0.0495 (0.0496)  loss_rpn_box_reg: 0.0684 (0.0709)  time: 0.5207  data: 0.0123  max mem: 6482\n",
      "Epoch: [1]  [2500/2698]  eta: 0:01:41  lr: 0.005000  loss: 0.4857 (0.5434)  loss_classifier: 0.1908 (0.2123)  loss_box_reg: 0.2083 (0.2111)  loss_objectness: 0.0387 (0.0494)  loss_rpn_box_reg: 0.0574 (0.0707)  time: 0.5271  data: 0.0108  max mem: 6482\n",
      "Epoch: [1]  [2600/2698]  eta: 0:00:50  lr: 0.005000  loss: 0.5147 (0.5429)  loss_classifier: 0.1967 (0.2122)  loss_box_reg: 0.2026 (0.2111)  loss_objectness: 0.0375 (0.0492)  loss_rpn_box_reg: 0.0483 (0.0705)  time: 0.5228  data: 0.0132  max mem: 6482\n",
      "Epoch: [1]  [2697/2698]  eta: 0:00:00  lr: 0.005000  loss: 0.5553 (0.5429)  loss_classifier: 0.2237 (0.2121)  loss_box_reg: 0.2083 (0.2110)  loss_objectness: 0.0499 (0.0492)  loss_rpn_box_reg: 0.0810 (0.0707)  time: 0.5080  data: 0.0111  max mem: 6482\n",
      "Epoch: [1] Total time: 0:23:15 (0.5174 s / it)\n",
      "creating index...\n",
      "index created!\n",
      "Test:  [  0/675]  eta: 0:12:43  model_time: 0.2112 (0.2112)  evaluator_time: 0.2297 (0.2297)  time: 1.1308  data: 0.6771  max mem: 6482\n",
      "Test:  [100/675]  eta: 0:05:30  model_time: 0.1534 (0.1580)  evaluator_time: 0.3292 (0.3907)  time: 0.5088  data: 0.0105  max mem: 6482\n",
      "Test:  [200/675]  eta: 0:05:05  model_time: 0.1577 (0.1579)  evaluator_time: 0.7600 (0.4630)  time: 0.9583  data: 0.0110  max mem: 6482\n",
      "Test:  [300/675]  eta: 0:03:49  model_time: 0.1581 (0.1586)  evaluator_time: 0.3734 (0.4321)  time: 0.5364  data: 0.0104  max mem: 6482\n",
      "Test:  [400/675]  eta: 0:02:42  model_time: 0.1569 (0.1592)  evaluator_time: 0.5488 (0.4095)  time: 0.7303  data: 0.0111  max mem: 6482\n",
      "Test:  [500/675]  eta: 0:01:43  model_time: 0.1593 (0.1591)  evaluator_time: 0.7852 (0.4108)  time: 0.9972  data: 0.0110  max mem: 6482\n",
      "Test:  [600/675]  eta: 0:00:45  model_time: 0.1591 (0.1592)  evaluator_time: 0.2256 (0.4331)  time: 0.4619  data: 0.0108  max mem: 6482\n",
      "Test:  [674/675]  eta: 0:00:00  model_time: 0.1599 (0.1594)  evaluator_time: 0.1524 (0.4184)  time: 0.3374  data: 0.0117  max mem: 6482\n",
      "Test: Total time: 0:06:43 (0.5984 s / it)\n",
      "Averaged stats: model_time: 0.1599 (0.1594)  evaluator_time: 0.1524 (0.4184)\n",
      "Accumulating evaluation results...\n",
      "DONE (t=1.90s).\n",
      "IoU metric: bbox\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.424\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.878\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.347\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.020\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.412\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.485\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.015\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.140\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.499\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.111\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.488\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.558\n",
      "Epoch: [2]  [   0/2698]  eta: 1:05:04  lr: 0.005000  loss: 0.5361 (0.5361)  loss_classifier: 0.2209 (0.2209)  loss_box_reg: 0.1936 (0.1936)  loss_objectness: 0.0409 (0.0409)  loss_rpn_box_reg: 0.0807 (0.0807)  time: 1.4473  data: 0.8596  max mem: 6482\n",
      "Epoch: [2]  [ 100/2698]  eta: 0:22:38  lr: 0.005000  loss: 0.4834 (0.4948)  loss_classifier: 0.1957 (0.1946)  loss_box_reg: 0.1953 (0.1963)  loss_objectness: 0.0353 (0.0394)  loss_rpn_box_reg: 0.0598 (0.0646)  time: 0.5164  data: 0.0121  max mem: 6482\n",
      "Epoch: [2]  [ 200/2698]  eta: 0:21:31  lr: 0.005000  loss: 0.5251 (0.4885)  loss_classifier: 0.2061 (0.1934)  loss_box_reg: 0.2121 (0.1920)  loss_objectness: 0.0435 (0.0398)  loss_rpn_box_reg: 0.0746 (0.0632)  time: 0.5182  data: 0.0122  max mem: 6482\n",
      "Epoch: [2]  [ 300/2698]  eta: 0:20:35  lr: 0.005000  loss: 0.4873 (0.4956)  loss_classifier: 0.1916 (0.1948)  loss_box_reg: 0.1882 (0.1945)  loss_objectness: 0.0358 (0.0412)  loss_rpn_box_reg: 0.0707 (0.0651)  time: 0.5132  data: 0.0110  max mem: 6482\n",
      "Epoch: [2]  [ 400/2698]  eta: 0:19:42  lr: 0.005000  loss: 0.5025 (0.4970)  loss_classifier: 0.2119 (0.1964)  loss_box_reg: 0.2028 (0.1936)  loss_objectness: 0.0427 (0.0419)  loss_rpn_box_reg: 0.0623 (0.0650)  time: 0.5084  data: 0.0100  max mem: 6482\n",
      "Epoch: [2]  [ 500/2698]  eta: 0:18:51  lr: 0.005000  loss: 0.4310 (0.4951)  loss_classifier: 0.1768 (0.1957)  loss_box_reg: 0.1796 (0.1930)  loss_objectness: 0.0345 (0.0411)  loss_rpn_box_reg: 0.0562 (0.0652)  time: 0.5137  data: 0.0106  max mem: 6482\n",
      "Epoch: [2]  [ 600/2698]  eta: 0:17:59  lr: 0.005000  loss: 0.5035 (0.4959)  loss_classifier: 0.1965 (0.1963)  loss_box_reg: 0.1866 (0.1922)  loss_objectness: 0.0362 (0.0413)  loss_rpn_box_reg: 0.0697 (0.0660)  time: 0.5121  data: 0.0110  max mem: 6482\n",
      "Epoch: [2]  [ 700/2698]  eta: 0:17:07  lr: 0.005000  loss: 0.4899 (0.4965)  loss_classifier: 0.1880 (0.1965)  loss_box_reg: 0.2052 (0.1926)  loss_objectness: 0.0356 (0.0414)  loss_rpn_box_reg: 0.0575 (0.0660)  time: 0.5128  data: 0.0107  max mem: 6482\n",
      "Epoch: [2]  [ 800/2698]  eta: 0:16:15  lr: 0.005000  loss: 0.4963 (0.4983)  loss_classifier: 0.1919 (0.1970)  loss_box_reg: 0.1945 (0.1934)  loss_objectness: 0.0404 (0.0418)  loss_rpn_box_reg: 0.0654 (0.0662)  time: 0.5184  data: 0.0115  max mem: 6482\n",
      "Epoch: [2]  [ 900/2698]  eta: 0:15:24  lr: 0.005000  loss: 0.5032 (0.4968)  loss_classifier: 0.1837 (0.1962)  loss_box_reg: 0.1969 (0.1928)  loss_objectness: 0.0379 (0.0418)  loss_rpn_box_reg: 0.0538 (0.0660)  time: 0.5180  data: 0.0111  max mem: 6482\n",
      "Epoch: [2]  [1000/2698]  eta: 0:14:32  lr: 0.005000  loss: 0.4533 (0.4963)  loss_classifier: 0.1744 (0.1961)  loss_box_reg: 0.1716 (0.1925)  loss_objectness: 0.0406 (0.0418)  loss_rpn_box_reg: 0.0541 (0.0659)  time: 0.5125  data: 0.0109  max mem: 6482\n",
      "Epoch: [2]  [1100/2698]  eta: 0:13:40  lr: 0.005000  loss: 0.4872 (0.4952)  loss_classifier: 0.1903 (0.1960)  loss_box_reg: 0.1935 (0.1923)  loss_objectness: 0.0368 (0.0413)  loss_rpn_box_reg: 0.0589 (0.0656)  time: 0.5097  data: 0.0101  max mem: 6482\n",
      "Epoch: [2]  [1200/2698]  eta: 0:12:49  lr: 0.005000  loss: 0.4603 (0.4941)  loss_classifier: 0.1819 (0.1955)  loss_box_reg: 0.1785 (0.1921)  loss_objectness: 0.0369 (0.0412)  loss_rpn_box_reg: 0.0535 (0.0652)  time: 0.5126  data: 0.0110  max mem: 6482\n",
      "Epoch: [2]  [1300/2698]  eta: 0:11:58  lr: 0.005000  loss: 0.4876 (0.4940)  loss_classifier: 0.1857 (0.1954)  loss_box_reg: 0.1748 (0.1920)  loss_objectness: 0.0345 (0.0413)  loss_rpn_box_reg: 0.0676 (0.0653)  time: 0.5105  data: 0.0102  max mem: 6482\n",
      "Epoch: [2]  [1400/2698]  eta: 0:11:06  lr: 0.005000  loss: 0.4705 (0.4928)  loss_classifier: 0.1771 (0.1947)  loss_box_reg: 0.1769 (0.1913)  loss_objectness: 0.0400 (0.0415)  loss_rpn_box_reg: 0.0659 (0.0653)  time: 0.5129  data: 0.0108  max mem: 6482\n",
      "Epoch: [2]  [1500/2698]  eta: 0:10:15  lr: 0.005000  loss: 0.4753 (0.4921)  loss_classifier: 0.1848 (0.1945)  loss_box_reg: 0.1894 (0.1910)  loss_objectness: 0.0318 (0.0413)  loss_rpn_box_reg: 0.0558 (0.0653)  time: 0.5162  data: 0.0107  max mem: 6482\n",
      "Epoch: [2]  [1600/2698]  eta: 0:09:23  lr: 0.005000  loss: 0.4227 (0.4918)  loss_classifier: 0.1638 (0.1946)  loss_box_reg: 0.1740 (0.1907)  loss_objectness: 0.0331 (0.0412)  loss_rpn_box_reg: 0.0539 (0.0653)  time: 0.5087  data: 0.0100  max mem: 6482\n",
      "Epoch: [2]  [1700/2698]  eta: 0:08:32  lr: 0.005000  loss: 0.4796 (0.4919)  loss_classifier: 0.2014 (0.1948)  loss_box_reg: 0.1789 (0.1908)  loss_objectness: 0.0320 (0.0412)  loss_rpn_box_reg: 0.0627 (0.0651)  time: 0.5095  data: 0.0102  max mem: 6482\n",
      "Epoch: [2]  [1800/2698]  eta: 0:07:40  lr: 0.005000  loss: 0.4840 (0.4927)  loss_classifier: 0.1820 (0.1951)  loss_box_reg: 0.1805 (0.1909)  loss_objectness: 0.0415 (0.0414)  loss_rpn_box_reg: 0.0593 (0.0653)  time: 0.5134  data: 0.0110  max mem: 6482\n",
      "Epoch: [2]  [1900/2698]  eta: 0:06:49  lr: 0.005000  loss: 0.4673 (0.4928)  loss_classifier: 0.1890 (0.1950)  loss_box_reg: 0.1885 (0.1911)  loss_objectness: 0.0349 (0.0414)  loss_rpn_box_reg: 0.0564 (0.0653)  time: 0.5082  data: 0.0102  max mem: 6482\n",
      "Epoch: [2]  [2000/2698]  eta: 0:05:58  lr: 0.005000  loss: 0.4326 (0.4915)  loss_classifier: 0.1792 (0.1947)  loss_box_reg: 0.1740 (0.1906)  loss_objectness: 0.0299 (0.0411)  loss_rpn_box_reg: 0.0516 (0.0650)  time: 0.5082  data: 0.0101  max mem: 6482\n",
      "Epoch: [2]  [2100/2698]  eta: 0:05:06  lr: 0.005000  loss: 0.4903 (0.4916)  loss_classifier: 0.1927 (0.1947)  loss_box_reg: 0.1909 (0.1907)  loss_objectness: 0.0374 (0.0411)  loss_rpn_box_reg: 0.0628 (0.0651)  time: 0.5225  data: 0.0138  max mem: 6482\n",
      "Epoch: [2]  [2200/2698]  eta: 0:04:15  lr: 0.005000  loss: 0.4911 (0.4920)  loss_classifier: 0.1859 (0.1948)  loss_box_reg: 0.1885 (0.1909)  loss_objectness: 0.0286 (0.0410)  loss_rpn_box_reg: 0.0711 (0.0653)  time: 0.5150  data: 0.0111  max mem: 6482\n",
      "Epoch: [2]  [2300/2698]  eta: 0:03:24  lr: 0.005000  loss: 0.4310 (0.4911)  loss_classifier: 0.1711 (0.1944)  loss_box_reg: 0.1766 (0.1906)  loss_objectness: 0.0333 (0.0409)  loss_rpn_box_reg: 0.0551 (0.0651)  time: 0.5093  data: 0.0107  max mem: 6482\n",
      "Epoch: [2]  [2400/2698]  eta: 0:02:32  lr: 0.005000  loss: 0.4318 (0.4911)  loss_classifier: 0.1765 (0.1946)  loss_box_reg: 0.1722 (0.1906)  loss_objectness: 0.0312 (0.0408)  loss_rpn_box_reg: 0.0568 (0.0651)  time: 0.5093  data: 0.0102  max mem: 6482\n",
      "Epoch: [2]  [2500/2698]  eta: 0:01:41  lr: 0.005000  loss: 0.4697 (0.4906)  loss_classifier: 0.1861 (0.1944)  loss_box_reg: 0.1890 (0.1905)  loss_objectness: 0.0318 (0.0406)  loss_rpn_box_reg: 0.0654 (0.0650)  time: 0.5095  data: 0.0104  max mem: 6482\n",
      "Epoch: [2]  [2600/2698]  eta: 0:00:50  lr: 0.005000  loss: 0.4772 (0.4902)  loss_classifier: 0.1909 (0.1943)  loss_box_reg: 0.1899 (0.1904)  loss_objectness: 0.0368 (0.0406)  loss_rpn_box_reg: 0.0561 (0.0650)  time: 0.5088  data: 0.0102  max mem: 6482\n",
      "Epoch: [2]  [2697/2698]  eta: 0:00:00  lr: 0.005000  loss: 0.4774 (0.4894)  loss_classifier: 0.1885 (0.1939)  loss_box_reg: 0.1860 (0.1902)  loss_objectness: 0.0364 (0.0404)  loss_rpn_box_reg: 0.0686 (0.0648)  time: 0.5066  data: 0.0102  max mem: 6482\n",
      "Epoch: [2] Total time: 0:23:04 (0.5130 s / it)\n",
      "creating index...\n",
      "index created!\n",
      "Test:  [  0/675]  eta: 0:12:07  model_time: 0.2287 (0.2287)  evaluator_time: 0.3329 (0.3329)  time: 1.0784  data: 0.4910  max mem: 6482\n",
      "Test:  [100/675]  eta: 0:04:56  model_time: 0.1495 (0.1547)  evaluator_time: 0.2749 (0.3372)  time: 0.4412  data: 0.0101  max mem: 6482\n",
      "Test:  [200/675]  eta: 0:04:42  model_time: 0.1534 (0.1540)  evaluator_time: 0.7288 (0.4184)  time: 0.9249  data: 0.0116  max mem: 6482\n",
      "Test:  [300/675]  eta: 0:03:32  model_time: 0.1538 (0.1548)  evaluator_time: 0.3256 (0.3912)  time: 0.4849  data: 0.0105  max mem: 6482\n",
      "Test:  [400/675]  eta: 0:02:29  model_time: 0.1525 (0.1551)  evaluator_time: 0.4860 (0.3670)  time: 0.6585  data: 0.0111  max mem: 6482\n",
      "Test:  [500/675]  eta: 0:01:35  model_time: 0.1550 (0.1549)  evaluator_time: 0.7489 (0.3673)  time: 0.9402  data: 0.0107  max mem: 6482\n",
      "Test:  [600/675]  eta: 0:00:42  model_time: 0.1546 (0.1550)  evaluator_time: 0.1897 (0.3902)  time: 0.4124  data: 0.0108  max mem: 6482\n",
      "Test:  [674/675]  eta: 0:00:00  model_time: 0.1588 (0.1552)  evaluator_time: 0.0946 (0.3749)  time: 0.3099  data: 0.0117  max mem: 6482\n",
      "Test: Total time: 0:06:12 (0.5512 s / it)\n",
      "Averaged stats: model_time: 0.1588 (0.1552)  evaluator_time: 0.0946 (0.3749)\n",
      "Accumulating evaluation results...\n",
      "DONE (t=1.67s).\n",
      "IoU metric: bbox\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.423\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.865\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.351\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.027\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.409\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.498\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.015\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.140\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.495\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.115\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.481\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.570\n",
      "Epoch: [3]  [   0/2698]  eta: 1:09:08  lr: 0.000500  loss: 0.4887 (0.4887)  loss_classifier: 0.2166 (0.2166)  loss_box_reg: 0.1575 (0.1575)  loss_objectness: 0.0308 (0.0308)  loss_rpn_box_reg: 0.0839 (0.0839)  time: 1.5377  data: 0.9091  max mem: 6482\n",
      "Epoch: [3]  [ 100/2698]  eta: 0:22:41  lr: 0.000500  loss: 0.4487 (0.4346)  loss_classifier: 0.1758 (0.1730)  loss_box_reg: 0.1931 (0.1696)  loss_objectness: 0.0304 (0.0341)  loss_rpn_box_reg: 0.0505 (0.0579)  time: 0.5165  data: 0.0104  max mem: 6482\n",
      "Epoch: [3]  [ 200/2698]  eta: 0:21:33  lr: 0.000500  loss: 0.3714 (0.4305)  loss_classifier: 0.1555 (0.1712)  loss_box_reg: 0.1537 (0.1683)  loss_objectness: 0.0315 (0.0335)  loss_rpn_box_reg: 0.0584 (0.0575)  time: 0.5099  data: 0.0105  max mem: 6482\n",
      "Epoch: [3]  [ 300/2698]  eta: 0:20:36  lr: 0.000500  loss: 0.3714 (0.4274)  loss_classifier: 0.1530 (0.1709)  loss_box_reg: 0.1604 (0.1683)  loss_objectness: 0.0209 (0.0326)  loss_rpn_box_reg: 0.0408 (0.0555)  time: 0.5087  data: 0.0109  max mem: 6482\n",
      "Epoch: [3]  [ 400/2698]  eta: 0:19:43  lr: 0.000500  loss: 0.4576 (0.4269)  loss_classifier: 0.1760 (0.1710)  loss_box_reg: 0.1698 (0.1672)  loss_objectness: 0.0332 (0.0329)  loss_rpn_box_reg: 0.0602 (0.0558)  time: 0.5113  data: 0.0107  max mem: 6482\n",
      "Epoch: [3]  [ 500/2698]  eta: 0:18:50  lr: 0.000500  loss: 0.3832 (0.4249)  loss_classifier: 0.1593 (0.1706)  loss_box_reg: 0.1608 (0.1660)  loss_objectness: 0.0302 (0.0328)  loss_rpn_box_reg: 0.0529 (0.0555)  time: 0.5107  data: 0.0100  max mem: 6482\n",
      "Epoch: [3]  [ 600/2698]  eta: 0:17:58  lr: 0.000500  loss: 0.4064 (0.4195)  loss_classifier: 0.1622 (0.1690)  loss_box_reg: 0.1600 (0.1642)  loss_objectness: 0.0274 (0.0318)  loss_rpn_box_reg: 0.0503 (0.0545)  time: 0.5153  data: 0.0121  max mem: 6482\n",
      "Epoch: [3]  [ 700/2698]  eta: 0:17:07  lr: 0.000500  loss: 0.4446 (0.4186)  loss_classifier: 0.1772 (0.1688)  loss_box_reg: 0.1726 (0.1640)  loss_objectness: 0.0373 (0.0315)  loss_rpn_box_reg: 0.0562 (0.0543)  time: 0.5220  data: 0.0120  max mem: 6482\n",
      "Epoch: [3]  [ 800/2698]  eta: 0:16:14  lr: 0.000500  loss: 0.4638 (0.4181)  loss_classifier: 0.1792 (0.1683)  loss_box_reg: 0.1776 (0.1637)  loss_objectness: 0.0369 (0.0317)  loss_rpn_box_reg: 0.0509 (0.0544)  time: 0.5106  data: 0.0102  max mem: 6482\n",
      "Epoch: [3]  [ 900/2698]  eta: 0:15:23  lr: 0.000500  loss: 0.4220 (0.4170)  loss_classifier: 0.1606 (0.1681)  loss_box_reg: 0.1609 (0.1633)  loss_objectness: 0.0281 (0.0313)  loss_rpn_box_reg: 0.0467 (0.0542)  time: 0.5093  data: 0.0103  max mem: 6482\n",
      "Epoch: [3]  [1000/2698]  eta: 0:14:31  lr: 0.000500  loss: 0.4169 (0.4167)  loss_classifier: 0.1608 (0.1679)  loss_box_reg: 0.1540 (0.1627)  loss_objectness: 0.0314 (0.0316)  loss_rpn_box_reg: 0.0498 (0.0545)  time: 0.5081  data: 0.0101  max mem: 6482\n",
      "Epoch: [3]  [1100/2698]  eta: 0:13:40  lr: 0.000500  loss: 0.3994 (0.4154)  loss_classifier: 0.1577 (0.1673)  loss_box_reg: 0.1613 (0.1621)  loss_objectness: 0.0223 (0.0317)  loss_rpn_box_reg: 0.0479 (0.0544)  time: 0.5087  data: 0.0101  max mem: 6482\n",
      "Epoch: [3]  [1200/2698]  eta: 0:12:48  lr: 0.000500  loss: 0.4392 (0.4160)  loss_classifier: 0.1775 (0.1675)  loss_box_reg: 0.1674 (0.1621)  loss_objectness: 0.0319 (0.0319)  loss_rpn_box_reg: 0.0545 (0.0545)  time: 0.5077  data: 0.0101  max mem: 6482\n",
      "Epoch: [3]  [1300/2698]  eta: 0:11:57  lr: 0.000500  loss: 0.4091 (0.4146)  loss_classifier: 0.1660 (0.1671)  loss_box_reg: 0.1570 (0.1613)  loss_objectness: 0.0219 (0.0319)  loss_rpn_box_reg: 0.0579 (0.0543)  time: 0.5158  data: 0.0103  max mem: 6482\n",
      "Epoch: [3]  [1400/2698]  eta: 0:11:05  lr: 0.000500  loss: 0.4286 (0.4136)  loss_classifier: 0.1576 (0.1665)  loss_box_reg: 0.1622 (0.1611)  loss_objectness: 0.0356 (0.0318)  loss_rpn_box_reg: 0.0599 (0.0543)  time: 0.5243  data: 0.0116  max mem: 6482\n",
      "Epoch: [3]  [1500/2698]  eta: 0:10:14  lr: 0.000500  loss: 0.4069 (0.4127)  loss_classifier: 0.1645 (0.1660)  loss_box_reg: 0.1582 (0.1607)  loss_objectness: 0.0299 (0.0317)  loss_rpn_box_reg: 0.0542 (0.0543)  time: 0.5123  data: 0.0105  max mem: 6482\n",
      "Epoch: [3]  [1600/2698]  eta: 0:09:23  lr: 0.000500  loss: 0.4045 (0.4117)  loss_classifier: 0.1630 (0.1656)  loss_box_reg: 0.1624 (0.1603)  loss_objectness: 0.0273 (0.0316)  loss_rpn_box_reg: 0.0506 (0.0542)  time: 0.5079  data: 0.0101  max mem: 6482\n",
      "Epoch: [3]  [1700/2698]  eta: 0:08:31  lr: 0.000500  loss: 0.4361 (0.4121)  loss_classifier: 0.1718 (0.1658)  loss_box_reg: 0.1655 (0.1606)  loss_objectness: 0.0315 (0.0316)  loss_rpn_box_reg: 0.0529 (0.0541)  time: 0.5080  data: 0.0108  max mem: 6482\n",
      "Epoch: [3]  [1800/2698]  eta: 0:07:40  lr: 0.000500  loss: 0.3978 (0.4110)  loss_classifier: 0.1529 (0.1653)  loss_box_reg: 0.1518 (0.1601)  loss_objectness: 0.0278 (0.0315)  loss_rpn_box_reg: 0.0535 (0.0541)  time: 0.5073  data: 0.0102  max mem: 6482\n",
      "Epoch: [3]  [1900/2698]  eta: 0:06:49  lr: 0.000500  loss: 0.4125 (0.4109)  loss_classifier: 0.1560 (0.1653)  loss_box_reg: 0.1520 (0.1599)  loss_objectness: 0.0386 (0.0316)  loss_rpn_box_reg: 0.0517 (0.0541)  time: 0.5079  data: 0.0106  max mem: 6482\n",
      "Epoch: [3]  [2000/2698]  eta: 0:05:57  lr: 0.000500  loss: 0.4364 (0.4106)  loss_classifier: 0.1676 (0.1652)  loss_box_reg: 0.1696 (0.1599)  loss_objectness: 0.0298 (0.0315)  loss_rpn_box_reg: 0.0509 (0.0540)  time: 0.5121  data: 0.0104  max mem: 6482\n",
      "Epoch: [3]  [2100/2698]  eta: 0:05:06  lr: 0.000500  loss: 0.4530 (0.4106)  loss_classifier: 0.1720 (0.1652)  loss_box_reg: 0.1688 (0.1598)  loss_objectness: 0.0288 (0.0316)  loss_rpn_box_reg: 0.0601 (0.0541)  time: 0.5161  data: 0.0122  max mem: 6482\n",
      "Epoch: [3]  [2200/2698]  eta: 0:04:15  lr: 0.000500  loss: 0.4044 (0.4103)  loss_classifier: 0.1601 (0.1651)  loss_box_reg: 0.1479 (0.1596)  loss_objectness: 0.0313 (0.0316)  loss_rpn_box_reg: 0.0597 (0.0541)  time: 0.5123  data: 0.0103  max mem: 6482\n",
      "Epoch: [3]  [2300/2698]  eta: 0:03:23  lr: 0.000500  loss: 0.4346 (0.4099)  loss_classifier: 0.1737 (0.1649)  loss_box_reg: 0.1761 (0.1594)  loss_objectness: 0.0300 (0.0315)  loss_rpn_box_reg: 0.0554 (0.0541)  time: 0.5083  data: 0.0102  max mem: 6482\n",
      "Epoch: [3]  [2400/2698]  eta: 0:02:32  lr: 0.000500  loss: 0.3631 (0.4101)  loss_classifier: 0.1479 (0.1650)  loss_box_reg: 0.1480 (0.1595)  loss_objectness: 0.0257 (0.0316)  loss_rpn_box_reg: 0.0484 (0.0541)  time: 0.5115  data: 0.0105  max mem: 6482\n",
      "Epoch: [3]  [2500/2698]  eta: 0:01:41  lr: 0.000500  loss: 0.3697 (0.4096)  loss_classifier: 0.1644 (0.1647)  loss_box_reg: 0.1395 (0.1593)  loss_objectness: 0.0245 (0.0314)  loss_rpn_box_reg: 0.0519 (0.0541)  time: 0.5082  data: 0.0107  max mem: 6482\n",
      "Epoch: [3]  [2600/2698]  eta: 0:00:50  lr: 0.000500  loss: 0.3723 (0.4090)  loss_classifier: 0.1585 (0.1646)  loss_box_reg: 0.1521 (0.1590)  loss_objectness: 0.0214 (0.0313)  loss_rpn_box_reg: 0.0480 (0.0541)  time: 0.5107  data: 0.0107  max mem: 6482\n",
      "Epoch: [3]  [2697/2698]  eta: 0:00:00  lr: 0.000500  loss: 0.4107 (0.4091)  loss_classifier: 0.1643 (0.1646)  loss_box_reg: 0.1605 (0.1591)  loss_objectness: 0.0286 (0.0313)  loss_rpn_box_reg: 0.0499 (0.0541)  time: 0.5134  data: 0.0127  max mem: 6482\n",
      "Epoch: [3] Total time: 0:23:03 (0.5127 s / it)\n",
      "creating index...\n",
      "index created!\n",
      "Test:  [  0/675]  eta: 0:11:54  model_time: 0.1897 (0.1897)  evaluator_time: 0.2850 (0.2850)  time: 1.0588  data: 0.5664  max mem: 6482\n",
      "Test:  [100/675]  eta: 0:05:00  model_time: 0.1482 (0.1517)  evaluator_time: 0.2780 (0.3447)  time: 0.4624  data: 0.0107  max mem: 6482\n",
      "Test:  [200/675]  eta: 0:04:42  model_time: 0.1488 (0.1506)  evaluator_time: 0.7465 (0.4220)  time: 0.9191  data: 0.0110  max mem: 6482\n",
      "Test:  [300/675]  eta: 0:03:32  model_time: 0.1490 (0.1515)  evaluator_time: 0.3466 (0.3934)  time: 0.4870  data: 0.0108  max mem: 6482\n",
      "Test:  [400/675]  eta: 0:02:29  model_time: 0.1470 (0.1517)  evaluator_time: 0.5125 (0.3694)  time: 0.6689  data: 0.0108  max mem: 6482\n",
      "Test:  [500/675]  eta: 0:01:35  model_time: 0.1504 (0.1515)  evaluator_time: 0.7706 (0.3704)  time: 0.9822  data: 0.0110  max mem: 6482\n",
      "Test:  [600/675]  eta: 0:00:42  model_time: 0.1522 (0.1513)  evaluator_time: 0.2035 (0.3936)  time: 0.4169  data: 0.0106  max mem: 6482\n",
      "Test:  [674/675]  eta: 0:00:00  model_time: 0.1551 (0.1515)  evaluator_time: 0.0859 (0.3784)  time: 0.2828  data: 0.0104  max mem: 6482\n",
      "Test: Total time: 0:06:11 (0.5506 s / it)\n",
      "Averaged stats: model_time: 0.1551 (0.1515)  evaluator_time: 0.0859 (0.3784)\n",
      "Accumulating evaluation results...\n",
      "DONE (t=1.60s).\n",
      "IoU metric: bbox\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.467\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.899\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.430\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.045\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.459\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.521\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.016\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.148\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.537\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.146\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.530\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.583\n",
      "Epoch: [4]  [   0/2698]  eta: 1:07:00  lr: 0.000500  loss: 0.4752 (0.4752)  loss_classifier: 0.1785 (0.1785)  loss_box_reg: 0.2243 (0.2243)  loss_objectness: 0.0182 (0.0182)  loss_rpn_box_reg: 0.0543 (0.0543)  time: 1.4901  data: 0.8427  max mem: 6482\n",
      "Epoch: [4]  [ 100/2698]  eta: 0:22:47  lr: 0.000500  loss: 0.3842 (0.3986)  loss_classifier: 0.1439 (0.1588)  loss_box_reg: 0.1503 (0.1541)  loss_objectness: 0.0207 (0.0300)  loss_rpn_box_reg: 0.0466 (0.0557)  time: 0.5096  data: 0.0107  max mem: 6482\n",
      "Epoch: [4]  [ 200/2698]  eta: 0:21:40  lr: 0.000500  loss: 0.3651 (0.3967)  loss_classifier: 0.1491 (0.1592)  loss_box_reg: 0.1363 (0.1524)  loss_objectness: 0.0220 (0.0309)  loss_rpn_box_reg: 0.0434 (0.0542)  time: 0.5112  data: 0.0110  max mem: 6482\n",
      "Epoch: [4]  [ 300/2698]  eta: 0:20:41  lr: 0.000500  loss: 0.3757 (0.3973)  loss_classifier: 0.1574 (0.1602)  loss_box_reg: 0.1542 (0.1535)  loss_objectness: 0.0247 (0.0301)  loss_rpn_box_reg: 0.0498 (0.0535)  time: 0.5079  data: 0.0104  max mem: 6482\n",
      "Epoch: [4]  [ 400/2698]  eta: 0:19:46  lr: 0.000500  loss: 0.3882 (0.3952)  loss_classifier: 0.1478 (0.1588)  loss_box_reg: 0.1566 (0.1537)  loss_objectness: 0.0256 (0.0293)  loss_rpn_box_reg: 0.0543 (0.0533)  time: 0.5112  data: 0.0109  max mem: 6482\n",
      "Epoch: [4]  [ 500/2698]  eta: 0:18:53  lr: 0.000500  loss: 0.4503 (0.3947)  loss_classifier: 0.1625 (0.1586)  loss_box_reg: 0.1629 (0.1539)  loss_objectness: 0.0273 (0.0289)  loss_rpn_box_reg: 0.0530 (0.0533)  time: 0.5151  data: 0.0104  max mem: 6482\n",
      "Epoch: [4]  [ 600/2698]  eta: 0:18:00  lr: 0.000500  loss: 0.3758 (0.3943)  loss_classifier: 0.1461 (0.1585)  loss_box_reg: 0.1507 (0.1537)  loss_objectness: 0.0257 (0.0291)  loss_rpn_box_reg: 0.0404 (0.0531)  time: 0.5243  data: 0.0126  max mem: 6482\n",
      "Epoch: [4]  [ 700/2698]  eta: 0:17:08  lr: 0.000500  loss: 0.4313 (0.3941)  loss_classifier: 0.1519 (0.1584)  loss_box_reg: 0.1751 (0.1537)  loss_objectness: 0.0223 (0.0290)  loss_rpn_box_reg: 0.0494 (0.0529)  time: 0.5124  data: 0.0106  max mem: 6482\n",
      "Epoch: [4]  [ 800/2698]  eta: 0:16:16  lr: 0.000500  loss: 0.3833 (0.3933)  loss_classifier: 0.1544 (0.1581)  loss_box_reg: 0.1583 (0.1539)  loss_objectness: 0.0188 (0.0287)  loss_rpn_box_reg: 0.0447 (0.0526)  time: 0.5095  data: 0.0108  max mem: 6482\n",
      "Epoch: [4]  [ 900/2698]  eta: 0:15:25  lr: 0.000500  loss: 0.3680 (0.3931)  loss_classifier: 0.1532 (0.1583)  loss_box_reg: 0.1394 (0.1535)  loss_objectness: 0.0199 (0.0287)  loss_rpn_box_reg: 0.0430 (0.0525)  time: 0.5109  data: 0.0110  max mem: 6482\n",
      "Epoch: [4]  [1000/2698]  eta: 0:14:33  lr: 0.000500  loss: 0.4159 (0.3941)  loss_classifier: 0.1653 (0.1588)  loss_box_reg: 0.1686 (0.1538)  loss_objectness: 0.0390 (0.0290)  loss_rpn_box_reg: 0.0544 (0.0525)  time: 0.5093  data: 0.0102  max mem: 6482\n",
      "Epoch: [4]  [1100/2698]  eta: 0:13:41  lr: 0.000500  loss: 0.4049 (0.3951)  loss_classifier: 0.1599 (0.1591)  loss_box_reg: 0.1516 (0.1541)  loss_objectness: 0.0292 (0.0290)  loss_rpn_box_reg: 0.0558 (0.0529)  time: 0.5132  data: 0.0112  max mem: 6482\n",
      "Epoch: [4]  [1200/2698]  eta: 0:12:50  lr: 0.000500  loss: 0.3884 (0.3945)  loss_classifier: 0.1532 (0.1589)  loss_box_reg: 0.1417 (0.1537)  loss_objectness: 0.0265 (0.0290)  loss_rpn_box_reg: 0.0536 (0.0529)  time: 0.5122  data: 0.0110  max mem: 6482\n",
      "Epoch: [4]  [1300/2698]  eta: 0:11:58  lr: 0.000500  loss: 0.4069 (0.3935)  loss_classifier: 0.1668 (0.1586)  loss_box_reg: 0.1472 (0.1534)  loss_objectness: 0.0280 (0.0288)  loss_rpn_box_reg: 0.0578 (0.0527)  time: 0.5228  data: 0.0122  max mem: 6482\n",
      "Epoch: [4]  [1400/2698]  eta: 0:11:07  lr: 0.000500  loss: 0.3879 (0.3933)  loss_classifier: 0.1630 (0.1585)  loss_box_reg: 0.1446 (0.1533)  loss_objectness: 0.0238 (0.0288)  loss_rpn_box_reg: 0.0477 (0.0526)  time: 0.5115  data: 0.0108  max mem: 6482\n",
      "Epoch: [4]  [1500/2698]  eta: 0:10:15  lr: 0.000500  loss: 0.4146 (0.3939)  loss_classifier: 0.1635 (0.1587)  loss_box_reg: 0.1532 (0.1535)  loss_objectness: 0.0308 (0.0289)  loss_rpn_box_reg: 0.0591 (0.0528)  time: 0.5081  data: 0.0108  max mem: 6482\n",
      "Epoch: [4]  [1600/2698]  eta: 0:09:24  lr: 0.000500  loss: 0.3894 (0.3938)  loss_classifier: 0.1456 (0.1585)  loss_box_reg: 0.1579 (0.1537)  loss_objectness: 0.0213 (0.0289)  loss_rpn_box_reg: 0.0489 (0.0527)  time: 0.5106  data: 0.0103  max mem: 6482\n",
      "Epoch: [4]  [1700/2698]  eta: 0:08:33  lr: 0.000500  loss: 0.3556 (0.3943)  loss_classifier: 0.1586 (0.1587)  loss_box_reg: 0.1408 (0.1538)  loss_objectness: 0.0208 (0.0289)  loss_rpn_box_reg: 0.0474 (0.0529)  time: 0.5115  data: 0.0106  max mem: 6482\n",
      "Epoch: [4]  [1800/2698]  eta: 0:07:41  lr: 0.000500  loss: 0.3949 (0.3939)  loss_classifier: 0.1575 (0.1587)  loss_box_reg: 0.1496 (0.1536)  loss_objectness: 0.0236 (0.0288)  loss_rpn_box_reg: 0.0393 (0.0528)  time: 0.5124  data: 0.0109  max mem: 6482\n",
      "Epoch: [4]  [1900/2698]  eta: 0:06:50  lr: 0.000500  loss: 0.4185 (0.3940)  loss_classifier: 0.1654 (0.1586)  loss_box_reg: 0.1592 (0.1536)  loss_objectness: 0.0331 (0.0289)  loss_rpn_box_reg: 0.0605 (0.0529)  time: 0.5157  data: 0.0114  max mem: 6482\n",
      "Epoch: [4]  [2000/2698]  eta: 0:05:58  lr: 0.000500  loss: 0.4049 (0.3937)  loss_classifier: 0.1558 (0.1586)  loss_box_reg: 0.1433 (0.1535)  loss_objectness: 0.0235 (0.0288)  loss_rpn_box_reg: 0.0541 (0.0529)  time: 0.5199  data: 0.0122  max mem: 6482\n",
      "Epoch: [4]  [2100/2698]  eta: 0:05:07  lr: 0.000500  loss: 0.3753 (0.3937)  loss_classifier: 0.1507 (0.1585)  loss_box_reg: 0.1560 (0.1536)  loss_objectness: 0.0207 (0.0288)  loss_rpn_box_reg: 0.0434 (0.0529)  time: 0.5094  data: 0.0110  max mem: 6482\n",
      "Epoch: [4]  [2200/2698]  eta: 0:04:15  lr: 0.000500  loss: 0.3810 (0.3937)  loss_classifier: 0.1563 (0.1585)  loss_box_reg: 0.1555 (0.1536)  loss_objectness: 0.0186 (0.0286)  loss_rpn_box_reg: 0.0512 (0.0529)  time: 0.5097  data: 0.0104  max mem: 6482\n",
      "Epoch: [4]  [2300/2698]  eta: 0:03:24  lr: 0.000500  loss: 0.4007 (0.3935)  loss_classifier: 0.1479 (0.1585)  loss_box_reg: 0.1498 (0.1534)  loss_objectness: 0.0287 (0.0286)  loss_rpn_box_reg: 0.0530 (0.0530)  time: 0.5085  data: 0.0103  max mem: 6482\n",
      "Epoch: [4]  [2400/2698]  eta: 0:02:33  lr: 0.000500  loss: 0.3567 (0.3931)  loss_classifier: 0.1291 (0.1583)  loss_box_reg: 0.1521 (0.1534)  loss_objectness: 0.0162 (0.0285)  loss_rpn_box_reg: 0.0419 (0.0529)  time: 0.5097  data: 0.0108  max mem: 6482\n",
      "Epoch: [4]  [2500/2698]  eta: 0:01:41  lr: 0.000500  loss: 0.4019 (0.3929)  loss_classifier: 0.1439 (0.1581)  loss_box_reg: 0.1515 (0.1533)  loss_objectness: 0.0311 (0.0285)  loss_rpn_box_reg: 0.0595 (0.0529)  time: 0.5100  data: 0.0107  max mem: 6482\n",
      "Epoch: [4]  [2600/2698]  eta: 0:00:50  lr: 0.000500  loss: 0.4045 (0.3925)  loss_classifier: 0.1611 (0.1580)  loss_box_reg: 0.1621 (0.1532)  loss_objectness: 0.0253 (0.0285)  loss_rpn_box_reg: 0.0608 (0.0529)  time: 0.5159  data: 0.0125  max mem: 6482\n",
      "Epoch: [4]  [2697/2698]  eta: 0:00:00  lr: 0.000500  loss: 0.3651 (0.3923)  loss_classifier: 0.1461 (0.1580)  loss_box_reg: 0.1395 (0.1531)  loss_objectness: 0.0204 (0.0284)  loss_rpn_box_reg: 0.0470 (0.0528)  time: 0.5183  data: 0.0139  max mem: 6482\n",
      "Epoch: [4] Total time: 0:23:06 (0.5138 s / it)\n",
      "creating index...\n",
      "index created!\n",
      "Test:  [  0/675]  eta: 0:12:42  model_time: 0.2005 (0.2005)  evaluator_time: 0.2764 (0.2764)  time: 1.1301  data: 0.6288  max mem: 6482\n",
      "Test:  [100/675]  eta: 0:04:51  model_time: 0.1467 (0.1504)  evaluator_time: 0.2795 (0.3308)  time: 0.4562  data: 0.0103  max mem: 6482\n",
      "Test:  [200/675]  eta: 0:04:39  model_time: 0.1465 (0.1505)  evaluator_time: 0.7033 (0.4139)  time: 0.9089  data: 0.0110  max mem: 6482\n",
      "Test:  [300/675]  eta: 0:03:31  model_time: 0.1497 (0.1516)  evaluator_time: 0.3507 (0.3895)  time: 0.5078  data: 0.0122  max mem: 6482\n",
      "Test:  [400/675]  eta: 0:02:29  model_time: 0.1465 (0.1520)  evaluator_time: 0.5106 (0.3702)  time: 0.6865  data: 0.0114  max mem: 6482\n",
      "Test:  [500/675]  eta: 0:01:35  model_time: 0.1497 (0.1519)  evaluator_time: 0.7793 (0.3741)  time: 0.9839  data: 0.0111  max mem: 6482\n",
      "Test:  [600/675]  eta: 0:00:42  model_time: 0.1531 (0.1518)  evaluator_time: 0.1907 (0.3994)  time: 0.4331  data: 0.0109  max mem: 6482\n",
      "Test:  [674/675]  eta: 0:00:00  model_time: 0.1549 (0.1520)  evaluator_time: 0.0901 (0.3870)  time: 0.2862  data: 0.0114  max mem: 6482\n",
      "Test: Total time: 0:06:20 (0.5637 s / it)\n",
      "Averaged stats: model_time: 0.1549 (0.1520)  evaluator_time: 0.0901 (0.3870)\n",
      "Accumulating evaluation results...\n",
      "DONE (t=1.66s).\n",
      "IoU metric: bbox\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.470\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.900\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.435\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.056\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.461\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.526\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.016\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.149\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.539\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.142\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.529\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.594\n"
     ]
    }
   ],
   "source": [
    "num_classes = 2\n",
    "train_dataset = WheatDataset(train_df, folds=[1, 2, 3, 4])\n",
    "train_loader = data_utils.DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=4, collate_fn=utils.collate_fn)\n",
    "\n",
    "val_dataset = WheatDataset(train_df, folds=[0])\n",
    "val_loader = data_utils.DataLoader(val_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4, collate_fn=utils.collate_fn)\n",
    "\n",
    "\n",
    "# move model to the right device\n",
    "model.to(DEVICE)\n",
    "\n",
    "# construct an optimizer\n",
    "params = [p for p in model.parameters() if p.requires_grad]\n",
    "optimizer = torch.optim.SGD(params, lr=0.005, momentum=0.9, weight_decay=0.0005)\n",
    "\n",
    "# and a learning rate scheduler\n",
    "lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1)\n",
    "\n",
    "for epoch in range(N_EPOCHS):\n",
    "    # train for one epoch, printing every 100 iterations\n",
    "    train_one_epoch(model, optimizer, train_loader, DEVICE, epoch, print_freq=100)\n",
    "    # update the learning rate\n",
    "    lr_scheduler.step()\n",
    "    # evaluate on the validation dataset\n",
    "    evaluate(model, val_loader, device=DEVICE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "!rm -rf *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "torch.save(model.state_dict(), 'fasterrcnn_resnet101_fold0.pth')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_bbox(bboxes, col, color='white'):\n",
    "    \n",
    "    for i in range(len(bboxes)):\n",
    "        # Create a Rectangle patch\n",
    "        rect = patches.Rectangle(\n",
    "            (bboxes[i][0], bboxes[i][1]),\n",
    "            bboxes[i][2] - bboxes[i][0], \n",
    "            bboxes[i][3] - bboxes[i][1], \n",
    "            linewidth=2, \n",
    "            edgecolor=color, \n",
    "            facecolor='none')\n",
    "\n",
    "        # Add the patch to the Axes\n",
    "        col.add_patch(rect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'device' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-14-e23ba2552ea7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      3\u001b[0m     \u001b[0mimage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcvtColor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCOLOR_BGR2RGB\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat32\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m     \u001b[0mimage\u001b[0m \u001b[0;34m/=\u001b[0m \u001b[0;36m255.0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m     \u001b[0mpreds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_numpy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtranspose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdevice\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m     \u001b[0mpred_bboxes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpreds\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'boxes'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcpu\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdetach\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnumpy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'device' is not defined"
     ]
    }
   ],
   "source": [
    "for img in os.listdir(TEST_DIR)[:5]:\n",
    "    image = cv2.imread(os.path.join(TEST_DIR, img), cv2.IMREAD_COLOR)\n",
    "    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n",
    "    image /= 255.0\n",
    "    preds = model([torch.from_numpy(np.transpose(image, (2, 0, 1))).to(device)])[0]\n",
    "    \n",
    "    pred_bboxes = preds['boxes'].cpu().detach().numpy()\n",
    "    pred_scores = preds['scores'].cpu().detach().numpy()\n",
    "    \n",
    "    mask = pred_scores >= 0.4\n",
    "    pred_scores = pred_scores[mask]\n",
    "    pred_bboxes = pred_bboxes[mask]\n",
    "    \n",
    "    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 10))\n",
    "    get_bbox(pred_bboxes, ax, color='red')\n",
    "    ax.imshow(image)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {
     "428cdcaa7dd24dd5aba5cfe18476e907": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "1.2.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "overflow_x": null,
       "overflow_y": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     },
     "646ff773af84452c8de33792cac4fdd9": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "1.2.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "overflow_x": null,
       "overflow_y": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     },
     "7380e9b0833845a5be0ae2e04cb3b04d": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "ProgressStyleModel",
      "state": {
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "ProgressStyleModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "StyleView",
       "bar_color": null,
       "description_width": "initial"
      }
     },
     "7fccbef02f5a4972b926ffe40d855b58": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "HTMLModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "1.5.0",
       "_view_name": "HTMLView",
       "description": "",
       "description_tooltip": null,
       "layout": "IPY_MODEL_646ff773af84452c8de33792cac4fdd9",
       "placeholder": "​",
       "style": "IPY_MODEL_931489626e9e4c578201ac8407ecfa36",
       "value": " 170M/170M [00:11&lt;00:00, 15.1MB/s]"
      }
     },
     "931489626e9e4c578201ac8407ecfa36": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "DescriptionStyleModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "StyleView",
       "description_width": ""
      }
     },
     "9695f0f8529249c99f32a69f6259ae17": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "FloatProgressModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "FloatProgressModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "1.5.0",
       "_view_name": "ProgressView",
       "bar_style": "success",
       "description": "100%",
       "description_tooltip": null,
       "layout": "IPY_MODEL_ebcdca5bfe3c4783accb5d955ccc0af6",
       "max": 178728960.0,
       "min": 0.0,
       "orientation": "horizontal",
       "style": "IPY_MODEL_7380e9b0833845a5be0ae2e04cb3b04d",
       "value": 178728960.0
      }
     },
     "e195f6be1869424fb31f6705ceaae55b": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HBoxModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "HBoxModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "1.5.0",
       "_view_name": "HBoxView",
       "box_style": "",
       "children": [
        "IPY_MODEL_9695f0f8529249c99f32a69f6259ae17",
        "IPY_MODEL_7fccbef02f5a4972b926ffe40d855b58"
       ],
       "layout": "IPY_MODEL_428cdcaa7dd24dd5aba5cfe18476e907"
      }
     },
     "ebcdca5bfe3c4783accb5d955ccc0af6": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "1.2.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "overflow_x": null,
       "overflow_y": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     }
    },
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
